package com.sh.shanhuo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gyf.immersionbar.ImmersionBar;
import com.sh.shanhuo.R;
import com.sh.shanhuo.activity.BaseActivity;
import com.sh.shanhuo.activity.ClickAction;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public abstract class BaseFragment<A extends BaseActivity> extends Fragment implements ClickAction {

    protected abstract int getLayoutId();

    protected void initImmersionBar() {
        ImmersionBar.with(this)
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .navigationBarColor(R.color.white)
                .navigationBarDarkIcon(true)
                .init();
    }

    protected boolean isUseEventBus() {
        return false;
    }

    protected boolean isBackgroundListener() {
        return false;
    }

    protected void initView() {
    }

    protected void initData() {
    }

    /**
     * app切换到后台
     */
    protected void runInBackground() {
    }

    public A mActivity;
    private View mRootView;
    /*是否加载过*/
    private boolean mLoading = false;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    public Context getContext() {
        return mActivity;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mActivity = (A) getActivity();
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mLoading = false;
        if (getLayoutId() > 0) {
            mRootView = inflater.inflate(getLayoutId(), null);
            return mRootView;
        } else {
            return null;
        }
    }

    @Override
    public void onDestroyView() {
        mLoading = false;
        mRootView = null;
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!mLoading) {
            mLoading = true;
            initFragment();
        }
    }

    /**
     * 这个 Fragment 是否已经加载过了
     */
    public boolean isLoading() {
        return mLoading;
    }

    @NonNull
    @Override
    public View getView() {
        return mRootView;
    }

    /**
     * 获取绑定的 Activity，防止出现 getActivity 为空
     */
    public A getAttachActivity() {
        return mActivity;
    }

    protected void initFragment() {
        initImmersionBar();
        initView();
        initData();
    }

    public <V extends View> V findViewById(@IdRes int id) {
        return mRootView.findViewById(id);
    }

    /**
     * 销毁当前 Fragment 所在的 Activity
     */
    public void finish() {
        if (mActivity != null && !mActivity.isFinishing()) {
            mActivity.finish();
        }
    }

    /**
     * Fragment 返回键被按下时回调
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 默认不拦截按键事件，回传给 Activity
        return false;
    }

}
